package com.news.web.servlet.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.service.NewsService;
import com.news.service.impl.NewsServiceImpl;

/**
 * Servlet implementation class AdminNewsDeleteAllServlet
 */
@WebServlet("/AdminNewsDeleteAllServlet")
public class AdminNewsDeleteAllServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 获取传入数据
		String[] newsIdStr = request.getParameterValues("newsIdAll");
		System.out.println("88888888888");
		// 创建NewsService实例
		NewsService newsService = new NewsServiceImpl();
//		for (String string : newsIdStr) {
//			System.out.println(string); 
//		}
		if (newsIdStr != null) {
		    int[] newsIds = new int[newsIdStr.length];
		    System.out.println(newsIds);
		    for (int i = 0; i < newsIdStr.length; i++) {
		    	//执行异常抛出，加入传入的str不能转为数字
		    	try {
		    		newsIds[i] = Integer.parseInt(newsIdStr[i]);
				} catch (NumberFormatException e) {
					// 如果失败，设置为null
					System.out.println("失败");
					request.setAttribute("newsDeleteBooleanAll", null);
					// 跳转回原页面
					request.getRequestDispatcher("/admin/adminNewsQueryAndDelete.jsp").forward(request, response);
					return;
				}
		    }
		    // 现在，userIds 数组中包含了 userIdStr 数组中的每个元素的整数值
			// 执行删除方法
		    System.out.println("11111111111");
			Boolean newsDeleteBooleanAll = newsService.deleteNews(newsIds);
			//将数据存入
			request.setAttribute( "newsDeleteBooleanAll",newsDeleteBooleanAll);
		}
		// 跳转回原页面
		request.getRequestDispatcher("/admin/adminNewsQueryAndDelete.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
